﻿using System;
using System.Data;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server.Search.Administration;

namespace SharePointSearchBench.Library
{

    /// <summary>
    /// Encapsulates data about a SharePoint search scope.
    /// </summary>
    public class SearchScope
    {
        string name;

        /// <summary>
        /// Constructs the object from a <see cref="System.Data.DataRow"/>.  
        /// This overload is used when creating the object
        /// from a web service call result.
        /// </summary>
        /// <param name="row"></param>
        public SearchScope(DataRow row)
        {
            this.name = (string)row["Name"];
        }

        /// <summary>
        /// Constructs the object from a <see cref="Microsoft.Office.Server.Search.Administration.Scope"/>.
        /// This overload is used when creating the object from an object model call result.
        /// </summary>
        /// <param name="scope"></param>
        public SearchScope(Scope scope)
        {
            this.name = scope.Name;
        }

        /// <summary>
        /// Gets the name.
        /// </summary>
        public string Name
        {
            get { return this.name; }
        }

        /// <summary>
        /// Displays the search scope's name.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.Name;
        }

        /// <summary>
        /// Extracts a collection of <see cref="SearchScope"/> objects from
        /// a <see cref="System.Data.DataSet"/>.  
        /// </summary>
        /// <param name="metadata"></param>
        /// <returns></returns>
        /// <remarks>This method assumes a certain DataSet schema - as normally
        /// returned by SharePoint web service operations.</remarks>
        public static Collection<SearchScope> GetScopesFromRawDataSet(DataSet metadata)
        {
            Collection<SearchScope> items = new Collection<SearchScope>();
            foreach(DataRow row in metadata.Tables[1].Rows)
            {
                items.Add(new SearchScope(row));
            }
            return items;
        }
    }
}
